package top.zhangjianyong.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;


import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author ： zhangjianyong
 * @date ： 2023/3/22 8:48 AM
 */
@Slf4j
public class HtmlUtil {

    public static Elements getTag(String html, String tag) {
        Document doc = Jsoup.parse(html);
        Elements elements = doc.getElementsByTag(tag);
        return elements;
    }

    public static List<String> getImgUrl(String html) {
        return getTagAttr(html, "img", "src");
    }

    public static List<String> getLinkUrl(String html) {
        return getTagAttr(html, "a", "href");
    }

    public static List<String> getVideoUrl(String html) {
        return getTagAttr(html, "video", "src");
    }

    public static List<String> getTagAttr(String html, String tag, String attr) {
        Document doc = Jsoup.parse(html);
        return doc.getElementsByTag(tag).eachAttr(attr);
    }

    /**
     * 格式化url
     *
     * @param list
     * @return
     */
    public static Set<String> removeBlankUrl(List<String> list) {
        return list.stream().map(s -> {
            if (StringUtils.isBlank(s)) {
                return s;
            }
            try {
                s = ConverPercent.decode(s);
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
            s = s.trim().replaceAll(" ", "")
                    .replaceAll("\t", "")
                    .replaceAll("\n", "")
                    .replaceAll("\r", "")
                    .toLowerCase();
            if (s.startsWith("http://.")) {
                s = s.replace("http://.", "http://");
            }
            return s;
        }).filter(s -> {
            if (StringUtils.isBlank(s)) {
                return false;
            }
            return RegexUtil
                    .matches(s, "^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://)(([A-Za-z0-9-~]+)\\.){2,}([A-Za-z0-9-~]+)");
        }).map(s -> {

            String p = UrlUtil.protocol(s).toLowerCase();
            String h = UrlUtil.host(s).toLowerCase();
            return p + "://" + h;
        }).map(url -> UrlUtil.index(url)).collect(Collectors.toSet());
    }


    public static void main(String[] args) throws UnsupportedEncodingException {
        List<String> list = new ArrayList<>();
        list.add("http://www.BAIDU.com/ss/aa/sd");
        Set<String> set = removeBlankUrl(list);
        System.out.println(set);

    }

    public static class HtmlHead {
        public String title;
        public String keywords;
        public String description;

        public HtmlHead() {
        }

        public HtmlHead(String title, String keywords, String description) {
            this.title = title;
            this.keywords = keywords;
            this.description = description;
        }
    }

    public static HtmlHead getHead(String body) {
        if (StringUtils.isBlank(body)) return new HtmlHead();
        Document doc = Jsoup.parse(body);
        String title = doc.getElementsByTag("title").text();
        if (StringUtils.isEmpty(title)) {
            title = doc.select("meta[property='og:title']").attr("content");
        }
        String keywords = doc.select("meta[name='keywords']").attr("content");
        if (StringUtils.isEmpty(keywords)) {
            keywords = doc.select("meta[property='og:keywords']").attr("content");
        }
        String description = doc.select("meta[name='description']").attr("content");
        if (StringUtils.isEmpty(description)) {
            description = doc.select("meta[property='og:description']").attr("content");
        }
        return new HtmlHead(title, keywords, description);
    }
}
